package com.smart.campus.school.service.impl;

import java.util.Date;
import java.util.List;
import com.smart.campus.common.utils.DateUtils;
import com.smart.campus.common.utils.StringUtils;
import com.smart.campus.framework.util.ShiroUtils;
import com.smart.campus.school.domain.*;
import com.smart.campus.school.domain.vo.CourseItemConfigVo;
import com.smart.campus.school.enums.MsgTypeEnum;
import com.smart.campus.school.service.*;
import com.smart.campus.system.domain.SysPost;
import com.smart.campus.system.domain.SysUser;
import com.smart.campus.system.service.ISysPostService;
import com.smart.campus.system.service.ISysUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.smart.campus.school.mapper.AdjustCourseMapper;
import com.smart.campus.common.core.text.Convert;

/**
 * 调课Service业务层处理
 * 
 * @author Zhihe.Li
 * @date 2020-08-15
 */
@Service
public class AdjustCourseServiceImpl implements IAdjustCourseService 
{
    @Autowired
    private AdjustCourseMapper adjustCourseMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IGradeService gradeService;
    @Autowired
    private IClassInfoService classInfoService;
    @Autowired
    private ISysPostService postService;

    @Autowired
    private IMsgService msgService;

    @Autowired
    private IAdjustCourseAdminService adjustCourseAdminService;

    @Autowired
    private ICourseItemConfigService courseItemConfigService;

    /**
     * 查询调课
     * 
     * @param id 调课ID
     * @return 调课
     */
    @Override
    public AdjustCourse selectAdjustCourseById(Long id)
    {
        return adjustCourseMapper.selectAdjustCourseById(id);
    }

    /**
     * 查询调课列表
     * 
     * @param adjustCourse 调课
     * @return 调课
     */
    @Override
    public List<AdjustCourse> selectAdjustCourseList(AdjustCourse adjustCourse)
    {
        return adjustCourseMapper.selectAdjustCourseList(adjustCourse);
    }

    /**
     * 新增调课
     * 
     * @param adjustCourse 调课
     * @return 结果
     */
    @Override
    public int insertAdjustCourse(AdjustCourse adjustCourse)
    {
        adjustCourse.setCreateTime(DateUtils.getNowDate());
        adjustCourse.setCreateBy(ShiroUtils.getLoginName());
        adjustCourse.setStatus(1);
        //如果没有选择具体课程节数 按全部一个老师安排
        if(adjustCourse.getCourseNo() == 0){

        }
        Long gradeId = adjustCourse.getGradeId();
        if(gradeId == null){
            CourseItemConfigVo config = new CourseItemConfigVo();
            config.setTeacherId(ShiroUtils.getUserId());
            List<CourseItemConfigVo> configVoList = courseItemConfigService.selectCourseItemConfigList(config);
            if(CollectionUtils.isNotEmpty(configVoList)){
                ClassInfo classInfo = classInfoService.selectClassInfoById(configVoList.get(0).getClazzId());
                gradeId = classInfo.getGradeId();
            }
        }

        //获取审批人
        AdjustCourseAdmin admin = new AdjustCourseAdmin();
        admin.setGradeId(gradeId);
        List<AdjustCourseAdmin> adminList = adjustCourseAdminService.selectAdjustCourseAdminList(admin);
        if(CollectionUtils.isNotEmpty(adminList)){
            String adminIds = adminList.get(0).getAdminIds();
            adjustCourse.setApproverIds(adminIds);
            if(StringUtils.isNotEmpty(adminIds)){
                String[] adminIdArray = adminIds.split(",");
                for (int i = 0; i < adminIdArray.length; i++) {
                    SysUser user = sysUserService.selectUserById(Long.valueOf(adminIdArray[i]));
                    Msg msg = new Msg();
                    msg.setUserType(1);
                    msg.setStatus(0);
                    msg.setCreateTime(new Date());
                    msg.setUserId(user.getUserId());
                    msg.setUserName(user.getUserName());
                    msg.setContent("您有一条调课待审批申请");
                    msg.setMsgType(MsgTypeEnum.ADJUST.getValue());
                    msg.setMsgLink("/school/course/approve");
                    msgService.insertMsg(msg);
                }
            }

        }
        return adjustCourseMapper.insertAdjustCourse(adjustCourse);
    }


    /**
     * 修改调课
     * 
     * @param adjustCourse 调课
     * @return 结果
     */
    @Override
    public int updateAdjustCourse(AdjustCourse adjustCourse)
    {
        adjustCourse.setUpdateTime(DateUtils.getNowDate());
        //adjustCourse.setCreateBy(ShiroUtils.getLoginName());
        return adjustCourseMapper.updateAdjustCourse(adjustCourse);
    }

    /**
     * 删除调课对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAdjustCourseByIds(String ids)
    {
        return adjustCourseMapper.deleteAdjustCourseByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除调课信息
     * 
     * @param id 调课ID
     * @return 结果
     */
    @Override
    public int deleteAdjustCourseById(Long id)
    {
        return adjustCourseMapper.deleteAdjustCourseById(id);
    }

    @Override
    public int insertAdjustCourseByWechat(AdjustCourse adjustCourse) {
        SysUser createBy = this.sysUserService.selectUserById(Long.parseLong(adjustCourse.getCreateBy()));
        SysUser teacher = this.sysUserService.selectUserById(adjustCourse.getTeacherId());
        Grade grade = this.gradeService.selectGradeById(adjustCourse.getGradeId());
        ClassInfo clazz = this.classInfoService.selectClassInfoById(adjustCourse.getClassId());
        if(clazz != null){
            adjustCourse.setClassName(clazz.getClassName());
        }
        if(grade != null){
            adjustCourse.setGradeName(grade.getGradeName());
        }
        adjustCourse.setTeacherName(teacher.getUserName());
        adjustCourse.setCreateTime(DateUtils.getNowDate());
        adjustCourse.setStatus(1);
        adjustCourse.setCreateBy(createBy.getUserName());

        //获取审批人
        Long gradeId = adjustCourse.getGradeId();
        if(gradeId == null){
            CourseItemConfigVo config = new CourseItemConfigVo();
            config.setTeacherId(createBy.getUserId());
            List<CourseItemConfigVo> configVoList = courseItemConfigService.selectCourseItemConfigList(config);
            if(CollectionUtils.isNotEmpty(configVoList)){
                ClassInfo classInfo = classInfoService.selectClassInfoById(configVoList.get(0).getClazzId());
                gradeId = classInfo.getGradeId();
            }
        }

        //获取审批人
        AdjustCourseAdmin admin = new AdjustCourseAdmin();
        admin.setGradeId(gradeId);
        List<AdjustCourseAdmin> adminList = adjustCourseAdminService.selectAdjustCourseAdminList(admin);
        if(CollectionUtils.isNotEmpty(adminList)){
            String adminIds = adminList.get(0).getAdminIds();
            adjustCourse.setApproverIds(adminIds);
            if(StringUtils.isNotEmpty(adminIds)){
                String[] adminIdArray = adminIds.split(",");
                for (int i = 0; i < adminIdArray.length; i++) {
                    SysUser user = sysUserService.selectUserById(Long.valueOf(adminIdArray[i]));
                    Msg msg = new Msg();
                    msg.setUserType(1);
                    msg.setStatus(0);
                    msg.setCreateTime(new Date());
                    msg.setUserId(user.getUserId());
                    msg.setUserName(user.getUserName());
                    msg.setContent("您有一条调课待审批申请");
                    msg.setMsgType(MsgTypeEnum.ADJUST.getValue());
                    msg.setMsgLink("/school/course/approve");
                    msgService.insertMsg(msg);
                }
            }

        }
        return adjustCourseMapper.insertAdjustCourse(adjustCourse);
    }

    @Override
    public List<AdjustCourse> selectAdjustCourseListByUserId(Long userId) {
        return adjustCourseMapper.selectAdjustCourseListByUserId(userId);
    }
}
